<!DOCTYPE html>
<!--    
# Copyright (c) 2011-2021 Contributors to the Eclipse Foundation
# 
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
# 
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
# 
# SPDX-License-Identifier: EPL-2.0
#
-->
<html lang="en">

<head>
  <title>WebSphere MQTT Web client Tutorial</title> 
  <link rel="stylesheet" href="../style.css" type="text/css" />
  <script type="text/javascript" src="../WebSocket/mqttws31.js"></script>
  <script type="text/javascript" src="../WebSocket/tutorial.js"></script>
</head>
<body onload="load()")>
	<h1><img src="../logo.png" />
	Messages.</h1>
	<p>The message can be an ArrayBuffer or a string. 
	The qos determines how reliably the message is delivered, qos=0 means the message may be lost of there is a network failure. qos=1 means that 
	even if there is network failure, or the client stops, the message is delieverd when the client restarts or reconnects. 
	qos=2 means that the message is always delivered as with qos=1, but it is never delievered more than once.
	<br><h2>Example</h2>
	<!-- Everything in the table below is required for JS to work and provide expected functionality -->
	<table>
		<tr>		
	    <textarea id='newCode' rows="25" cols="100">		
client = new Messaging.Client(location.hostname, Number(location.port), "clientId");
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
client.connect({onSuccess:onConnect});

function onConnect() {
  // Once a connection has been made, make a subscription and send a message.
  console.log("onConnect");
  client.subscribe("/World");

  var buffer = new ArrayBuffer(5);
  var byteStream = new Uint8Array(buffer); 
  
  byteStream[0] = 0x48;
  byteStream[1] = 0x65;
  byteStream[2] = 0x6c;
  byteStream[3] = 0x6c;
  byteStream[4] = 0x6f;
  message = new Messaging.Message(buffer);
  message.destinationName = "/World";
  message.qos = 2;
  client.send(message); 
};
function onConnectionLost(reason) {
  if (reason)
    console.log("ConnectionLost:"+reason);
};
function onFailure(context,code,reason) {
    console.log("onFailure:"+context+" "+code+" "+reason);
};
function onMessageArrived(message) {
  console.log("onMessageArrived: payload="+message.payloadString+ " qos="+message.qos);
  client.disconnect(); 
};		
	</textarea>		
	</tr>
	<tr>
	<br>
	<input type='button' onclick='Try()' value='Click me to try.'>
	The Console output is shown below.
	</tr>
	<tr><div id='page'></div>			
	<form>
	<textarea id="console" readonly='readonly' rows="5" cols="100"></textarea>
	</form></tr>
		
	</table>
	
<div>
  <p><a href="Tutorial31_Start.html">Back to the start.</a></p> 
  <p><a href="Tutorial31_Subscribe.html">More about subscribing.</a></p>
  <p><a href="../index.html">Home.</a></p>
</div>
</body>
</html>
